from deepdiff import DeepDiff
from deepdiff.helper import CannotCompare


class CoreDiff(DeepDiff):
    def __init__(self, t1, t2, compare_mode='==', **kwargs) -> None:
        self.compare_mode = compare_mode
        t1 = t1.split(',') if (isinstance(t1, str)) else t1
        t2 = t2.split(',') if (isinstance(t2, str)) else t2
        super().__init__(t1, t2, **kwargs)

    def compare(self):
        try:
            if (self.t1 and self.t2):
                # 类型变化的差异，即两个对象的类型不同。
                type_changes = self.get('type_changes')
                # 数值变化的差异，即两个对象相同位置的值不同。
                values_changed = self.get('values_changed')
                set_item_added = self.get('set_item_added')
                set_item_removed = self.get('set_item_removed')
                attribute_added = self.get('attribute_added')
                attribute_removed = self.get('attribute_removed')
                repetition_change = self.get('repetition_change')
                # 可迭代对象中新增的元素。
                iterable_item_added = self.get('iterable_item_added')
                iterable_item_moved = self.get('iterable_item_moved')
                # 可迭代对象中被移除的元素。
                iterable_item_removed = self.get('iterable_item_removed')
                # 字典中新增的键值对。
                dictionary_item_added = self.get('dictionary_item_added')
                # 字典中被移除的键值对。
                dictionary_item_removed = self.get('dictionary_item_removed')
                if (self.compare_mode == '>'):
                    return True if (not type_changes and not values_changed and not iterable_item_added and (iterable_item_removed or repetition_change)) else False
                elif (self.compare_mode == '>='):
                    return True if (not type_changes and not values_changed and not iterable_item_added and (iterable_item_removed or repetition_change)) or not self else False
                elif (self.compare_mode == '<'):
                    return True if (not type_changes and not values_changed and not iterable_item_removed and (iterable_item_added or repetition_change)) else False
                elif (self.compare_mode == '<='):
                    return True if (not type_changes and not values_changed and not iterable_item_removed and (iterable_item_added or repetition_change)) or not self else False
                elif (self.compare_mode == '!='):
                    return True if self else False
                else:
                    return False if self else True
            else:
                return False
        except Exception:
            raise CannotCompare() from None

    def __str__(self) -> str:
        return super().__str__()